package com.global.gbinventory.barang.controller;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.global.gbinventory.barang.controller.GudangController.BtnCloseListener;
import com.global.gbinventory.barang.controller.GudangController.BtnDeleteListener;
import com.global.gbinventory.barang.controller.GudangController.BtnNewListener;
import com.global.gbinventory.barang.controller.GudangController.BtnSaveListener;
import com.global.gbinventory.barang.controller.GudangController.TableGudangListener;
import com.global.gbinventory.barang.tablemodel.BarangTableModel;
import com.global.gbinventory.barang.util.CONSTANT;
import com.global.gbinventory.barang.util.ImageFilter;
import com.global.gbinventory.barang.util.ImagePreview;
import com.global.gbinventory.barang.util.Status;
import com.global.gbinventory.barang.view.BarangFrame;
import com.global.gbinventory.data.view.KaryawanFrame;
import com.global.gbinventory.exception.ExistedDataException;
import com.global.gbinventory.exception.StarFieldsNotFilledException;
import com.global.gbinventory.main.controller.MainController;

public class BarangController {
	
	private MainController main;
	private BarangListController listController;
	private BarangFrame frame;

	private String oldKode;
	
	public BarangController(MainController main){
		this.main = main;
		oldKode = CONSTANT.EMPTY_STRING;
	}
	
	public void start(){
		initialize();
		frame.displayFrame(true);
		main.addFrameToDesktopPane(frame);
		frame.moveToFront();
	}
	
	private void initialize(){
		this.frame = new BarangFrame();
		this.listController = main.getBarangListController();
		addListeners();
		
		
		String[] arr = createCmbBoxModel(main.getSatuanController().getAllSatuanNames());
		DefaultComboBoxModel satuanModel = new DefaultComboBoxModel(arr);
		frame.setCmbBoxSatuanModel(satuanModel);
		frame.setCmbBoxSatuanSelectedIndex(0);
		
		arr = createCmbBoxModel(main.getGudangController().getAllGudangNames());
		DefaultComboBoxModel gudangModel = new DefaultComboBoxModel(arr);
		frame.setCmbBoxGudangModel(gudangModel);
		frame.setCmbBoxGudangSelectedIndex(0);
		
	}
	
	private String[] createCmbBoxModel(String[] array){
		String[] res = new String[array.length + 1];
		res[0] = CONSTANT.NOTHING_CHOSEN;
		for(int i = 1; i <= array.length; i++){
			res[i] = array[i - 1];
		}
		return res;
	}
	
	private void addListeners(){
		frame.addBtnNewListener(new BtnNewListener());
		frame.addBtnEditListener(new BtnEditListener());
		frame.addBtnDeleteListener(new BtnDeleteListener());
		frame.addBtnSaveListener(new BtnSaveListener());
		frame.addBtnCancelListener(new BtnCancelListener());
		frame.addBtnShowListListener(new BtnShowListListener());
		frame.addBtnShowGrupListListener(new BtnShowGrupListListener());
		frame.addBtnShowMerekListListener(new BtnShowMerekListListener());
		frame.addBtnPilihGambarListener(new BtnPilihGambarListener());
	}
	
	class BtnNewListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			frame.setTextFieldEditable();
			frame.requestFocusTxtFldKode();
			
//			listController.setSelectedRow(-1);
			activateSave();
		}
		
	}
	
	class BtnEditListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
//			oldKode = frame.getKode();
//			frame.setTextFieldEditable();
//			frame.requestFocusTxtFldKode();
//			activateSave();
		}
		
	}
	
	class BtnDeleteListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
//			String title = "Konfirmasi Penghapusan";
//			String message = "Anda yakin mau menghapus karyawan \"" + frame.getNama() + " \" ?";
//			int ans = JOptionPane.showConfirmDialog(frame, message, title, JOptionPane.YES_NO_OPTION);
//			
//			if(ans == JOptionPane.YES_OPTION){
//				listController.removeKaryawan();
//				
//				title = "Penghapusan Berhasil";
//				message = "Karyawan \"" + frame.getNama() + " \" berhasil dihapus.";
//				JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
//				
//				cancel();
//			}
		}
		
	}
	
	class BtnSaveListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
//			String kode = frame.getKode();
//			String nama = frame.getNama();
//			String alamat = frame.getAlamat();
//			String kota = frame.getKota();
//			String kodepos = frame.getKodePos();
//			String provinsi = frame.getProvinsi();
//			String telepon = frame.getTelepon();
//			String hp = frame.getHP();
//			String email = frame.getEmail();
//			String keterangan = frame.getKeterangan();
//			
//			try{
//				if(!isAllowedKode(kode)){
//					throw new ExistedDataException();
//				}
//				if(!isStarFieldsFilled()){
//					throw new StarFieldsNotFilledException();
//				}
//				
//				listController.updateKaryawan(kode, nama, alamat, kota, 
//						kodepos, provinsi, telepon, hp, email, keterangan);
//				
//				String title = "Save Karyawan Berhasil";
//				String message = "Data karyawan berhasil disimpan";
//				JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
//				
//				cancel();
//				
//			} catch(ExistedDataException e1){
//				e1.printError(frame);
//				
//			} catch(StarFieldsNotFilledException e2){
//				e2.printError(frame);
//			} 
			
		}
		
	}
	
	class BtnCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			cancel();
		}
		
	}
	
	class BtnShowListListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
//			listController.start(false);
		}
		
	}
	
	class BtnShowGrupListListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			main.getGrupController().start(true);
		}
		
	}
	
	class BtnShowMerekListListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			main.getMerekController().start(true);
		}
		
	}
	
	class BtnPilihGambarListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			JFileChooser fc = frame.getPilihGambarFC();
			
			//Add a custom file filter and disable the default
			//(Accept All) file filter
			fc.addChoosableFileFilter(new ImageFilter());
			fc.setAcceptAllFileFilterUsed(false);
		
			//Add the preview pane
			fc.setAccessory(new ImagePreview(fc));
			
			//Show it
			int returnVal = fc.showDialog(frame, "Pilih");
			
			//Process the results
			if(returnVal == JFileChooser.APPROVE_OPTION){
				File file = fc.getSelectedFile();
				ImageIcon img = null;
				ImageIcon temp = new ImageIcon(file.getPath());
				if(temp != null){
					if(temp.getIconWidth() > 160){
						img = new ImageIcon(temp.getImage().getScaledInstance(160, -1, Image.SCALE_DEFAULT));
					}else if(temp.getIconHeight() > 195){
						img = new ImageIcon(temp.getImage().getScaledInstance(-1, 195, Image.SCALE_DEFAULT));
					}
				}
				
				frame.setGambar(img);

			}else{
				System.out.println("Attachment cancelled by user!!");
			}
			
			//Reset the file chooser for the next time it's shown
			fc.setSelectedFile(null);
		}
			
	}
	
	private boolean isAllowedKode(String kode){
//		String existedKode = listController.isExistedKode(kode);
//		if(oldKode.equalsIgnoreCase(existedKode) || 
//				CONSTANT.EMPTY_STRING.equalsIgnoreCase(existedKode)){
//			return true;
//		}
		return false;
	}
	
	private boolean isStarFieldsFilled(){
//		if(frame.getKode().equals(CONSTANT.EMPTY_STRING) || frame.getNama().equals(CONSTANT.EMPTY_STRING) 
//				|| frame.getAlamat().equals(CONSTANT.EMPTY_STRING)){
//			return false;
//		}
//		
		return true;
	}
	
	public void fillGrupData(String grup, String subgrup){
		frame.setGrup(grup);
		frame.setSubgrup(subgrup);
	}
	
	public void fillMerekData(String merek){
		frame.setMerek(merek);
	}
	
	public void fillData(String kode, String nama, String alamat, String kota, String kodepos,
			String provinsi, String telepon, String hp, String email, String keterangan){
//		frame.setKode(kode);
//		frame.setNama(nama);
//		frame.setAlamat(alamat);
//		frame.setKota(kota);
//		frame.setKodePos(kodepos);
//		frame.setProvinsi(provinsi);
//		frame.setTelepon(telepon);
//		frame.setHP(hp);
//		frame.setEmail(email);
//		frame.setKeterangan(keterangan);
		
	}
	
	private void activateSave(){
		frame.setEnabledBtnNew(false);
		frame.setEnabledBtnEdit(false);
		frame.setEnabledBtnDelete(false);
		frame.setEnabledBtnSave(true);
		frame.setEnabledBtnCancel(true);
		frame.setEnabledBtnShowList(false);
		frame.setEnabledBtnShowGrupList(true);
		frame.setEnabledBtnShowMerekList(true);
	}
	
	public void activateEditDelete(){
//		frame.setEnabledBtnNew(false);
//		frame.setEnabledBtnEdit(true);
//		frame.setEnabledBtnDelete(true);
//		frame.setEnabledBtnSave(false);
//		frame.setEnabledBtnCancel(true);
//		frame.setEnabledBtnShowList(true);
	}
	
	private void cancel(){
		frame.clearTextField();
		frame.setTextFieldNonEditable();
		frame.setEnabledBtnNew(true);
		frame.setEnabledBtnEdit(false);
		frame.setEnabledBtnDelete(false);
		frame.setEnabledBtnSave(false);
		frame.setEnabledBtnCancel(false);
		frame.setEnabledBtnShowList(true);
		frame.setEnabledBtnShowGrupList(false);
		frame.setEnabledBtnShowMerekList(false);
//		listController.setSelectedRow(-1);
	}
}
